﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class PaymentController : BaseController
    {
        public string action = string.Empty;
        public string order_no = string.Empty;
        public string order_type = string.Empty;
        public decimal order_amount = 0;

        public Orderconfig orderConfig; //订单配置
        public Users userModel; //用户
        public Orders orderModel; //购物
        public User_recharge rechargeModel; //充值
        public Payment payModel; //支付

        public IUser_rechargeService _user_recharge { get; set; }
        public IPaymentService _payment { get; set; }
        public IOrdersService _orders { get; set; }
        public IActionResult Index()
        {
            orderConfig= Common.XMLConfig.Orderconfig.loadConfig(_env.ContentRootPath);
            //取得处事类型
            action = Request.Query["action"].FirstOrDefault()==null?"login": Request.Query["action"].FirstOrDefault();
            order_no = Request.Query["order_no"].FirstOrDefault()==null?"": Request.Query["order_no"].FirstOrDefault();
            if (order_no.ToUpper().StartsWith("R")) //充值订单
            {
                order_type = CMSEnums.AmountTypeEnum.Recharge.ToString().ToLower();
            }
            else if (order_no.ToUpper().StartsWith("B")) //商品订单
            {
                order_type = CMSEnums.AmountTypeEnum.BuyGoods.ToString().ToLower();
            }
            else if (order_no.ToUpper().StartsWith("P")) //商户支付
            {
                order_type = CMSEnums.AmountTypeEnum.PayShop.ToString().ToLower();
            }
            userModel = base.GetUserInfo(); //取得用户登录信息
            switch (action)
            {
                case "confirm":
                    if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(order_no))
                    {
                        return ToError("出错啦，URL传输参数有误！");
                    }
                    //是否需要支持匿名购物                    
                    if (orderConfig.anonymous == 0 || order_no.ToUpper().StartsWith("R"))
                    {
                        if (userModel == null)
                        {
                            //用户未登录
                            return Redirect("/Web/Payment/Index?action=login");
                        }
                    }
                    else if (userModel == null)
                    {
                        userModel = new Users();
                    }
                    //检查订单的类型(充值或购物)
                    if (order_no.ToUpper().StartsWith("R")) //充值订单
                    {
                        rechargeModel = _user_recharge.GetModel(order_no);
                        if (rechargeModel == null)
                        {
                            return ToError("出错啦，订单号不存在或已删除！");
                        }
                        //检查订单号是否已支付
                        if (rechargeModel.status == 1)
                        {
                            return Redirect("/Web/Payment/Index?action=succeed&order_no=" + rechargeModel.recharge_no);
                        }
                        //检查支付方式
                        payModel = _payment.GetModel(rechargeModel.payment_id);
                        if (payModel == null)
                        {
                            return ToError("出错啦，支付方式不存在或已删除！");
                        }
                        //检查是否线上支付
                        if (payModel.type == 2)
                        {
                            return ToError("出错啦，账户充值不允许线下支付！");
                        }
                        order_amount = rechargeModel.amount; //订单金额
                    }
                    else if (order_no.ToUpper().StartsWith("B")) //商品订单
                    {
                        //检查订单是否存在
                        orderModel = _orders.GetModel(order_no);
                        if (orderModel == null)
                        {
                            return ToError("出错啦，订单号不存在或已删除！");
                        }
                        //检查是否已支付过
                        if (orderModel.payment_status == 2)
                        {
                            return Redirect("/Web/Payment/Index?action=succeed&order_no=" + orderModel.order_no);
                        }
                        //检查支付方式
                        payModel = _payment.GetModel(orderModel.payment_id);
                        if (payModel == null)
                        {
                            return ToError("出错啦，支付方式不存在或已删除！");
                        }
                        //检查是否线下付款
                        if (orderModel.payment_status == 0)
                        {
                            return Redirect("/Web/Payment/Index?action=succeed&order_no=" + orderModel.order_no);
                        }
                        //检查是否积分换购，直接跳转成功页面
                        if (orderModel.order_amount == 0)
                        {
                            //修改订单状态
                            bool result = _orders.UpdateField(orderModel.order_no, "status=2,payment_status=2,payment_time='" + DateTime.Now + "'");
                            if (!result)
                            {
                                return Redirect("/Web/Payment/Index?action=error");
                            }
                            return Redirect("/Web/Payment/Index?action=succeed&order_no=" + orderModel.order_no);
                        }
                        order_amount = orderModel.order_amount; //订单金额
                    }
                    else
                    {
                        return ToError("出错啦，找不到您要提交的订单类型！");
                    }
                    break;
                case "succeed":
                    //检查订单的类型(充值或购物)
                    if (order_no.ToUpper().StartsWith("R")) //充值订单
                    {
                        rechargeModel = _user_recharge.GetModel(order_no);
                        if (rechargeModel == null)
                        {
                            return ToError("出错啦，订单号不存在或已删除！");
                        }

                    }
                    else if (order_no.ToUpper().StartsWith("B")) //商品订单
                    {
                        orderModel = _orders.GetModel(order_no);
                        if (orderModel == null)
                        {
                            return ToError("出错啦，订单号不存在或已删除！");
                        }
                    }
                    else if (order_no.ToUpper().StartsWith("P")) //商户订单
                    {
                        orderModel = new Orders();
                    }
                    else
                    {
                        return ToError("出错啦，找不到您要提交的订单类型！");
                    }
                    break;
            }
            ViewData["orderModel"] = orderModel;
            ViewData["payModel"] = payModel;
            ViewBag.action = action;
            ViewBag.order_type = order_type;
            ViewBag.order_amount = order_amount;
            ViewBag.order_no = order_no;
            ViewBag.user_name = userModel.user_name;
            return View();
        }
    }
}